/*
 * Lexer for turandot
 */

%option case-insensitive

%{
#include "tparse.h"

extern int lineno;
extern int yylval;

void skip_line();
void skip_comment();

%}

%s STRING
%s DOCCOMMENT

%%

<INITIAL>{
module				return MODULE;
use				return USE;

[a-zA-Z_][a-zA-Z0-9_]*		return IDENT;
[0-9]+				yylval = atoi(yytext); return NUMBER;
\<-				return LARROW;
\(				return LPAREN;
\)				return RPAREN;
"["				return LBRACKET;
"]"				return RBRACKET;
\{				return LBRACE;
\}				return RBRACE;
\'[^']\'			return QCHAR;				
\"				BEGIN(STRING);

\+				return ADD;
-				return SUB;
\*				return MULT;
\/				return DIV;

!=				return NEQUALS;
=				return EQUALS;

!				return NOT;
&&				return AND;
\|\|				return OR;

&				return BAND;
\|				return BOR;
\^				return BXOR;

\.				return DOT;

;				return SEMI;

[ \t]+				/* ignore whitespace */
\n				lineno++;

--				skip_line();
"{--"				skip_comment(); /* TODO read documentation */
"{-"				skip_comment();

.				yyerror("bad token");
}

<STRING>{
\"				BEGIN(INITIAL); return QSTRING;
\n				lineno++; REJECT;
.				/* TODO add to the string */
}

<DOCCOMMENT>{

}

%%

// skip to the end of a line (usually thanks to a comment)
void skip_line() {
	while (input() != '\n');
}

// skip a multi-line comment
void skip_comment() {
start:
	while (input() != '-');
	if (input() != '}') {
		unput(yytext[yyleng - 1]);
		goto start;
	}
}

